package com.yztc.orangeentertainment.ui.main.findfragment.findadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindGradeBean;
import com.yztc.orangeentertainment.bean.findbean.FindInteractBean;
import com.yztc.orangeentertainment.bean.findbean.FindOptionsBean;
import com.yztc.orangeentertainment.bean.findbean.FindVoteBean;
import com.yztc.orangeentertainment.bean.findbean.FindVoteimgBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 */
public class FindVoteRlvAdapter extends RecyclerView.Adapter<FindVoteRlvAdapter.VoteViewHolder> {
    private Context context;
    private List<FindInteractBean> list;
    int length;

    public FindVoteRlvAdapter(Context context, List<FindInteractBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public VoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        VoteViewHolder holder = new VoteViewHolder(LayoutInflater.from(context).inflate(R.layout
                .find_vote, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(final VoteViewHolder holder, final int position) {
        holder.tv_vote.setText(list.get(position).getTitle());
        FindVoteBean voteBean = list.get(position).getVote();
        final FindGradeBean gradeBean = list.get(position).getGrade();

        if (voteBean != null) {
            final List<FindOptionsBean> optionsBeens = voteBean.getOptions();
            if (position % list.size() == 0) {
                if (optionsBeens.get(position).getTitle() != null && !"".equals(optionsBeens.get
                        (position).getTitle()) && optionsBeens.get(position + 1).getTitle() !=
                        null && !"".equals(optionsBeens
                        .get(position + 1).getTitle())) {
                    holder.vote_one.setText(optionsBeens.get(position).getTitle());
                    holder.vote_one.setBackgroundResource(R.drawable.find_textstytleone);

                    holder.vote_two.setText(optionsBeens.get(position + 1).getTitle());
                    holder.vote_two.setBackgroundResource(R.drawable.find_textstytleone);

                    final int total = list.get(position).getVote().getTotal();
                    final double onenum = optionsBeens.get(position).getNumber();
                    final double twonum = optionsBeens.get(position + 1).getNumber();

                    holder.vote_one.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            holder.voteShowtwo.setVisibility(View.VISIBLE);
                            holder.voteShowone.setVisibility(View.GONE);
                            holder.voteTvone.setText(optionsBeens.get(position).getTitle());
                            holder.voteTvtwo.setText(optionsBeens.get(position + 1)
                                    .getTitle());

                            int a = (int) Math.rint((onenum + 1) / (total + 1) * 100);
                            int b = (int) Math.rint((twonum) / (total + 1) * 100);
                            holder.voteNumone.setText(a + "%");
                            holder.voteNumtwo.setText(b + "%");

                            //计算第一个按钮的点击控件的宽度


                            holder.votePbone.setBackgroundColor(context.getResources().getColor(R
                                    .color.hotpink));
                            holder.votePbtwo.setBackgroundColor(context.getResources().getColor(R
                                    .color.hotpink));
                        }
                    });

                    holder.vote_two.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            holder.voteShowtwo.setVisibility(View.VISIBLE);
                            holder.voteShowone.setVisibility(View.GONE);
                            holder.voteTvone.setText(optionsBeens.get(position).getTitle());
                            holder.voteTvtwo.setText(optionsBeens.get(position + 1)
                                    .getTitle());

                            int a = (int) Math.rint((onenum) / (total + 1) * 100);
                            int b = (int) Math.rint((twonum + 1) / (total + 1) * 100);
                            holder.voteNumone.setText(a + "%");
                            holder.voteNumtwo.setText(b + "%");

                            //计算第二个按钮的

                            holder.votePbone.setBackgroundColor(context.getResources().getColor(R
                                    .color.hotpink));
                            holder.votePbtwo.setBackgroundColor(context.getResources().getColor(R
                                    .color.hotpink));
                        }
                    });
                }
            } else {
                if (optionsBeens.get(position - 1).getTitle() != null && !"".equals(optionsBeens
                        .get(position - 1).getTitle()) && optionsBeens.get(position).getTitle()
                        != null && !"".equals(optionsBeens.get
                        (position).getTitle())) {
                    holder.vote_one.setText(optionsBeens.get(position - 1).getTitle());
                    holder.vote_one.setBackgroundResource(R.drawable.find_textstytletwo);

                    holder.vote_two.setText(optionsBeens.get(position).getTitle());
                    holder.vote_two.setBackgroundResource(R.drawable.find_textstytletwo);

                    final int total = list.get(position).getVote().getTotal();
                    final double onenum = optionsBeens.get(position - 1).getNumber();
                    final double twonum = optionsBeens.get(position).getNumber();

                    holder.vote_one.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.voteShowtwo.setVisibility(View.VISIBLE);
                            holder.voteShowone.setVisibility(View.GONE);
                            holder.voteTvone.setText(optionsBeens.get(position - 1).getTitle());
                            holder.voteTvtwo.setText(optionsBeens.get(position)
                                    .getTitle());

                            int a = (int) Math.rint((onenum + 1) / (total + 1) * 100);
                            int b = (int) Math.rint((twonum) / (total + 1) * 100);
                            holder.voteNumone.setText(a + "%");
                            holder.voteNumtwo.setText(b + "%");

                            //计算第一个按钮的宽度

                            holder.votePbone.setBackgroundColor(context.getResources().getColor(R
                                    .color.lightskyblue));
                            holder.votePbtwo.setBackgroundColor(context.getResources().getColor(R
                                    .color.lightskyblue));
                        }
                    });

                    holder.vote_two.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.voteShowtwo.setVisibility(View.VISIBLE);
                            holder.voteShowone.setVisibility(View.GONE);
                            holder.voteTvone.setText(optionsBeens.get(position - 1).getTitle());
                            holder.voteTvtwo.setText(optionsBeens.get(position)
                                    .getTitle());

                            int a = (int) Math.rint((onenum) / (total + 1) * 100);
                            int b = (int) Math.rint((twonum + 1) / (total + 1) * 100);
                            holder.voteNumone.setText(a + "%");
                            holder.voteNumtwo.setText(b + "%");

                            //计算第二个按钮点击时两个kongjia控价的宽度


                            holder.votePbone.setBackgroundColor(context.getResources().getColor(R
                                    .color.lightskyblue));
                            holder.votePbtwo.setBackgroundColor(context.getResources().getColor(R
                                    .color.lightskyblue));
                        }
                    });
                }
            }
            FindVoteimgBean voteimgBean = voteBean.getVoteimg();
            if (voteimgBean != null) {
                holder.iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams
                        .MATCH_PARENT, 800));
                Glide.with(context).load(voteimgBean.getSrc()).into(holder.iv);
            }
        } else {
            holder.newshow.setVisibility(View.VISIBLE);
            holder.oldshow.setVisibility(View.GONE);

            Glide.with(context).load(gradeBean.getVoteimg().getSrc()).into(holder.newIv);
            holder.newTv.setText(list.get(position).getTitle());

            holder.newSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    length = progress;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });

            holder.newBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.newBtn.setVisibility(View.GONE);
                    holder.newBtntv.setVisibility(View.VISIBLE);
                    holder.newSeekbar.setEnabled(false);
                    holder.newBtntv.setText("平均得分是" + (int) ((gradeBean.getTotalscore() + length)
                            / (gradeBean.getMembers() + 1)));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class VoteViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.vote_showone)
        LinearLayout voteShowone;
        @InjectView(R.id.vote_tvone)
        TextView voteTvone;
        @InjectView(R.id.vote_tvtwo)
        TextView voteTvtwo;
        @InjectView(R.id.vote_showtwo)
        LinearLayout voteShowtwo;
        @InjectView(R.id.vote_pbone)
        TextView votePbone;
        @InjectView(R.id.vote_numone)
        TextView voteNumone;
        @InjectView(R.id.vote_pbtwo)
        TextView votePbtwo;
        @InjectView(R.id.vote_numtwo)
        TextView voteNumtwo;
        @InjectView(R.id.rl_one)
        RelativeLayout rlOne;
        @InjectView(R.id.rl_two)
        RelativeLayout rlTwo;

        @InjectView(R.id.new_iv)
        ImageView newIv;
        @InjectView(R.id.new_tv)
        TextView newTv;
        @InjectView(R.id.new_seekbar)
        SeekBar newSeekbar;
        @InjectView(R.id.new_btn)
        Button newBtn;
        @InjectView(R.id.new_btntv)
        TextView newBtntv;
        @InjectView(R.id.newshow)
        RelativeLayout newshow;
        @InjectView(R.id.oldshow)
        LinearLayout oldshow;

        private TextView tv_vote, vote_one, vote_two;
        private ImageView iv;

        public VoteViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);

            tv_vote = (TextView) itemView.findViewById(R.id.find_vote);
            vote_one = (TextView) itemView.findViewById(R.id.findvote_tvone);
            vote_two = (TextView) itemView.findViewById(R.id.findvote_tvtwo);
            iv = (ImageView) itemView.findViewById(R.id.findvote_iv);
        }
    }
}
